//
//  ZFK_slideDetailViewController.m
//  Drops
//
//  Created by SHG on 16/6/1.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_slideDetailViewController.h"
#import "ZFK_slide.h"
#import "ZFK_slideTableViewCell.h"
#import "UIKit+AFNetworking.h"

@interface ZFK_slideDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)ZFK_slide *slide;

@end

@implementation ZFK_slideDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.slideArr = [NSMutableArray array];
    
//    [self createData];
    [self createView];
}

- (void)createView {
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 50, 40);
    [backButton setImage:[UIImage imageNamed:@"back.png"] forState:0];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.mainScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainScrollView];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width / 3 * 2)];
    image.backgroundColor = [UIColor lightGrayColor];
    [self.mainScrollView addSubview:image];
    [image setImageWithURL:[NSURL URLWithString:self.imgUrl]];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, image.frame.size.height / 6 * 5, WIDTH - 20, image.frame.size.height / 7)];
//    titleLabel.backgroundColor = [UIColor redColor];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = [UIColor whiteColor];
    [image addSubview:titleLabel];
    titleLabel.text = self.titled;
    
    UIView *userView = [[UIView alloc] initWithFrame:CGRectMake(0, image.frame.size.height, WIDTH, titleLabel.frame.size.height * 3 / 2)];
    userView.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:userView];
    
    UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, userView.frame.size.height - 10, userView.frame.size.height - 10)];
    userImage.layer.cornerRadius = (userView.frame.size.height - 10) / 2;
    userImage.layer.masksToBounds = YES;
//    userImage.backgroundColor = [UIColor redColor];
    [userView addSubview:userImage];
    [userImage setImageWithURL:[NSURL URLWithString:self.avatar]];
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(userView.frame.size.height - 10 + 15, (userView.frame.size.height - 10) / 3, userView.frame.size.width / 2, (userView.frame.size.height - 10) / 2)];
//    userName.backgroundColor = [UIColor greenColor];
    [userView addSubview:userName];
    userName.text = self.userName;
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [textLabel setNumberOfLines:0];
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
//    CGSize size = CGSizeMake(WIDTH, 2000);
//    CGSize labelSize = [textLabel sizeThatFits:CGSizeMake(WIDTH, MAXFLOAT)];
    textLabel.frame = CGRectMake(5, image.frame.size.height + userView.frame.size.height + 10, WIDTH - 10, 100);
    NSLog(@"%f",textLabel.frame.origin.y);
    textLabel.text = self.text;
    [textLabel sizeToFit];
    [self.mainScrollView addSubview:textLabel];
    NSLog(@"1111111%f",textLabel.frame.origin.y);
    self.mainScrollView.contentSize = CGSizeMake(0, image.frame.size.height + userView.frame.size.height + textLabel.frame.size.height + 113 + 40);
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZFK_slideTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZFK_slideTableViewCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.image setImageWithURL:[NSURL URLWithString:self.imgUrl]];
    cell.titleLabel.text = self.titled;
    [cell.userImage setImageWithURL:[NSURL URLWithString:self.avatar]];
    cell.userName.text = self.userName;
    cell.text.text = self.text;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    CGSize size = [self.cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return self.view.frame.size.height * 2;
}

- (void)createData {
    NSDictionary *param = @{@"version":@"2.9", @"uid":@"25512642", @"token":@"e89ba406540046ccbfc816bb142cda852bc282b04bc844458cf9bf29899028b8", @"activityId":[NSString stringWithFormat:@"%@", self.activityId], @"sortby":@"time", @"sincePostId":@"0", @"lastLikeCount":@"", @"offset":@"0"};
    
    [[AFHTTPSessionManager manager] POST:@"http://www.fotoplace.cc/api2/home/activity_detail.php" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.slideArr = [NSMutableArray array];
        
        NSDictionary *tempDic = responseObject[@"data"][@"activity"];
//        ZFK_slide *slide = [[ZFK_slide alloc] init];
//        [slide setValuesForKeysWithDictionary:tempDic[@"imgUrl"]];
        
        self.imgUrl = [NSString stringWithFormat:@"%@", tempDic[@"imgUrl"]];
        self.titled = [NSString stringWithFormat:@"%@", tempDic[@"title"]];
        self.avatar = [NSString stringWithFormat:@"%@", tempDic[@"avatar"]];
        self.userName = [NSString stringWithFormat:@"%@", tempDic[@"userName"]];
        self.text = [NSString stringWithFormat:@"%@", tempDic[@"text"]];
        NSLog(@"urllll:%@", self.imgUrl);
        
        NSLog(@"dic--%@", [tempDic[@"personCount"] class]);
        
//        [self.slideArr addObject:slide];
        
//        NSLog(@"slide:%@", self.slide.imgUrl);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
//    [self.mainTableView reloadData];
}

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
